/**
 * Copyright &copy; 2016 splat  All rights reserved.
 */
package com.contractpage.sys;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.ConfigUtil;
import com.contractpage.common.persistence.Page;
import com.contractpage.common.servlet.ValidateCodeServlet;
import com.contractpage.common.web.BaseController;
import com.contractpage.util.CookieUtils;
import com.contractpage.util.StringUtils;





/**
 * 登录Controller
 * @author splat
 * @version 2013-5-31
 */
@Controller
public class LoginController extends BaseController{
	
	//团队
	class Team{
		private String teamname;
		private String teamcode;
		private Integer teampeoples;
		public String getTeamname() {
			return teamname;
		}
		public void setTeamname(String teamname) {
			this.teamname = teamname;
		}
		public String getTeamcode() {
			return teamcode;
		}
		public void setTeamcode(String teamcode) {
			this.teamcode = teamcode;
		}
		public Integer getTeampeoples() {
			return teampeoples;
		}
		public void setTeampeoples(Integer teampeoples) {
			this.teampeoples = teampeoples;
		}
	}
	//营业部
	class Branch{
		private String branchname;
		private String branchcode;
		private Integer branchpeoples;
		public String getBranchname() {
			return branchname;
		}
		public void setBranchname(String branchname) {
			this.branchname = branchname;
		}
		public String getBranchcode() {
			return branchcode;
		}
		public void setBranchcode(String branchcode) {
			this.branchcode = branchcode;
		}
		public Integer getBranchpeoples() {
			return branchpeoples;
		}
		public void setBranchpeoples(Integer branchpeoples) {
			this.branchpeoples = branchpeoples;
		}
		
		
	}
	class BigArea{
		private String areaname;
		private String areacode;
		public String getAreaname() {
			return areaname;
		}
		public void setAreaname(String areaname) {
			this.areaname = areaname;
		}
		public String getAreacode() {
			return areacode;
		}
		public void setAreacode(String areacode) {
			this.areacode = areacode;
		}
	}
	/*@Autowired
	private EmpService empService;*/
	/**
	 * 验证码 
	 * @throws IOException 
	 */
	@RequestMapping(value = "code")
	public void code(HttpServletRequest request, HttpServletResponse response) throws IOException{
		ValidateCodeServlet validateCodeServlet = new ValidateCodeServlet();
		validateCodeServlet.createImage(request, response);
	}
	
	/**
	 * 验证码 
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping(value = "checkCode")
	public String checkCode(String validateCode,HttpServletRequest request) throws IOException{
		 if(ValidateCodeServlet.validate(request, validateCode)){
			return "true"; 
		 }
		 return "false";
	}
	
	@ResponseBody
	@RequestMapping(value = "teamList")
	public Object teamList(String teamname,HttpServletRequest request ,HttpServletResponse response){
		List<Team> list = new ArrayList<>();
		Page page = new Page<>(request, response);
		Team team = new Team();
		team.setTeamcode("HD1233");
		team.setTeamname("一柱擎天");
		team.setTeampeoples(11);
		list.add(team);
		page.setList(list);
		return page;
	}
	@ResponseBody
	@RequestMapping(value = "areaList")
	public Object areaList(String areaname,HttpServletRequest request ,HttpServletResponse response){
		List<BigArea> list = new ArrayList<>();
		Page page = new Page<>(request, response);
		BigArea area = new BigArea();
		area.setAreacode("HA2323112");
		area.setAreaname("西南");
		list.add(area);
		page.setList(list);
		return page;
	}
	@ResponseBody
	@RequestMapping(value = "branchList")
	public Object branchList(String branchname,HttpServletRequest request ,HttpServletResponse response){
		List<Branch> list = new ArrayList<>();
		Page page = new Page<>(request, response);
		Branch branch = new Branch();
		branch.setBranchcode("HB232123");
		branch.setBranchname("红袖添香");
		branch.setBranchpeoples(12);
		list.add(branch);
		page.setList(list);
		return page;
	}
	/**
	 * 管理登录
	 * @throws Exception 
	 */
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		// 如果已经登录，则跳转到管理首页
		return "redirect:" + ConfigUtil.getValue("sso.web.site");
	}
	
	@RequestMapping(value = "index" )
	public String index1(Model model, HttpServletRequest request, HttpServletResponse response) {
		//EmpVO emp = UserUtils.getUser();
		//model.addAttribute("emp", emp);
		return "index";
	}
	   /**
     * @Title 登出PC
     * @method  logoutPC
     * @param request
     * @param response
     * @throws
     * @author  sunyan
     * @date    2017-08-11
     */
	@RequestMapping(value = { "logoutPC"})
	@ResponseBody
	  public String logoutPC(HttpServletRequest request,HttpServletResponse response) {
       /* ResultData resultData = new ResultData();
        //CommonUtil.logOut(request, response);
        Cookie tokenCookie = Tool.ReadCookieMap(request).get("token");
        String token = tokenCookie==null?"":tokenCookie.getValue();
        try {
            String jwtCode = empService.verificationToken(token).getCode();

            Emp emp = empService.selectByCode(jwtCode);
            if (emp == null) {

                resultData.setStatus(CodeConts.PARAM_LEGAL);
                resultData.setMessage("用户不存在");
                response.sendRedirect(ConfigUtil.getValue("sso.web.site"));
            }
            //4、删除cookie
            Cookie cookie = new Cookie("token",null);
            cookie.setMaxAge(0);
            cookie.setPath("/");
            cookie.setDomain(ConfigUtil.getValue("sso.web.domain"));
            response.addCookie(cookie);
            //5、返回
            resultData.setStatus(CodeConts.SUCCESS);
            resultData.setMessage("登出成功 ");     
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
            return "redirect:" + ConfigUtil.getValue("sso.web.site");
        } catch (Exception e) {
            resultData.setStatus(CodeConts.FAILURE);
            resultData.setMessage("登出异常");
            return "redirect:" + ConfigUtil.getValue("sso.web.site");
        }*/
		return null;
    }
	/**
	 * 登录失败，真正登录的POST请求由Filter完成
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String loginFail(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/sys/sysLogin";
	}

	/**
	 * 登录成功，进入管理首页
	 */
	@RequestMapping(value = "")
	public String index(Model model,HttpServletRequest request, HttpServletResponse response) {
		return "index";
	}
	
	/**
	 * 获取主题方案
	 */
	@RequestMapping(value = "/theme/{theme}")
	public String getThemeInCookie(@PathVariable String theme, HttpServletRequest request, HttpServletResponse response){
		if (StringUtils.isNotBlank(theme)){
			CookieUtils.setCookie(response, "theme", theme);
		}else{
			theme = CookieUtils.getCookie(request, "theme");
		}
		return "redirect:"+request.getParameter("url");
	}
	
	/**
	 * 是否是验证码登录
	 * @param useruame 用户名
	 * @param isFail 计数加1
	 * @param clean 计数清零
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean isValidateCodeLogin(String useruame, boolean isFail, boolean clean){
		/*Map<String, Integer> loginFailMap = (Map<String, Integer>)CacheUtils.get("loginFailMap");
		if (loginFailMap==null){
			loginFailMap = Maps.newHashMap();
			//CacheUtils.put("loginFailMap", loginFailMap);
		}
		Integer loginFailNum = loginFailMap.get(useruame);
		if (loginFailNum==null){
			loginFailNum = 0;
		}
		if (isFail){
			loginFailNum++;
			loginFailMap.put(useruame, loginFailNum);
		}
		if (clean){
			loginFailMap.remove(useruame);
		}
		return loginFailNum >= 3;*/
		return false;
	}
}